package com.ts.animal.controller;

import com.ts.animal.domain.SaleRecordDO;
import com.ts.animal.service.SaleRecordService;
import com.ts.common.annotation.Log;
import com.ts.common.utils.PageInfos;
import com.ts.common.utils.PageUtils;
import com.ts.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 销售记录表
 *
 * @author bobby
 * @email bobby@126.com
 * @date 2020-02-04 22:02:22
 */

@Controller
@RequestMapping("/animal/saleRecord")
public class SaleRecordController {
    @Autowired
    private SaleRecordService saleRecordService;

    @GetMapping()
    @RequiresPermissions("animal:saleRecord:saleRecord")
    String SaleRecord() {
        return "animal/saleRecord/saleRecord";
    }

    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("animal:saleRecord:saleRecord")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        PageUtils pageUtils = new PageInfos(params) {
            public List inject(Map params) {
                return saleRecordService.exList(params);
            }
        }.query().lowerUnderscore();
        return pageUtils;
    }


    @GetMapping("/add")
    @RequiresPermissions("animal:saleRecord:add")
    String add() {
        return "animal/saleRecord/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("animal:saleRecord:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        SaleRecordDO saleRecord = saleRecordService.get(id);
        model.addAttribute("saleRecord", saleRecord);
        return "animal/saleRecord/edit";
    }

    /**
     * 保存 售卖出库
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("animal:saleRecord:add")
    public Result save(SaleRecordDO saleRecord) {
        if (saleRecordService.save(null, saleRecord) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 修改
     */
    @Log("修改出库信息")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("animal:saleRecord:edit")
    public Result update(SaleRecordDO saleRecord) {
        saleRecordService.update(null, saleRecord);
        return Result.ok();
    }

    /**
     * 删除
     */
    @Log("删除出库信息")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("animal:saleRecord:remove")
    public Result remove(Integer id) {
        if (saleRecordService.remove(id) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 删除
     */
    @Log("批量删除出库信息")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("animal:saleRecord:batchRemove")
    public Result remove(@RequestParam("ids[]") Integer[] ids) {
        saleRecordService.batchRemove(ids);
        return Result.ok();
    }

}
